package com.craftplay.util;


import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
import org.dom4j.DocumentException;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.lang.reflect.Field;
import java.util.*;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class CraftPlayUtil {
    public static String[] IMAGE_FILE_EXTD = new String[] {"png", "bmp", "jpg", "jpeg"};
    public static boolean isFileAllowed(String fileName) {
        for (String ext : IMAGE_FILE_EXTD) {
            if (ext.equals(fileName)) {
                return true;
            }
        }
        return false;
    }

//

//    private static boolean needCDATA(Class<?> targetClass, String fieldAlias) {
//        boolean cdata = false;
//        // first, scan self
//        cdata = existsCDATA(targetClass, fieldAlias);
//        if (cdata)
//            return cdata;
//        // if cdata is false, scan supperClass until java.lang.Object
//        Class<?> superClass = targetClass.getSuperclass();
//        while (!superClass.equals(Object.class)) {
//            cdata = existsCDATA(superClass, fieldAlias);
//            if (cdata)
//                return cdata;
//            superClass = superClass.getClass().getSuperclass();
//        }
//        return false;
//    }

//    private static boolean existsCDATA(Class<?> clazz, String fieldAlias) {
//        if ("MediaId".equals(fieldAlias)) {
//            return true; // 特例添加 morning99
//        }
//        // scan fields
//        Field[] fields = clazz.getDeclaredFields();
//        for (Field field : fields) {
//            // 1. exists XStreamCDATA
//            if (field.getAnnotation(XStreamCDATA.class) != null) {
//                XStreamAlias xStreamAlias = field.getAnnotation(XStreamAlias.class);
//                // 2. exists XStreamAlias
//                if (null != xStreamAlias) {
//                    if (fieldAlias.equals(xStreamAlias.value()))// matched
//                        return true;
//                } else {// not exists XStreamAlias
//                    if (fieldAlias.equals(field.getName()))
//                        return true;
//                }
//            }
//        }
//        return false;
//    }


    public static Map<String, String> parseRequestXmlToMap(HttpServletRequest request) throws Exception {
        // 解析结果存储在HashMap中
        Map<String, String> resultMap;
        InputStream inputStream = request.getInputStream();
        resultMap = parseInputStreamToMap(inputStream);
        return resultMap;
    }

    /**
     * 将输入流中的XML解析为Map
     *
     * @param inputStream
     * @return
     * @throws DocumentException
     * @throws IOException
     */
    public static Map<String, String> parseInputStreamToMap(InputStream inputStream) throws DocumentException, IOException {
        // 解析结果存储在HashMap中
        Map<String, String> map = new HashMap<String, String>();
        // 读取输入流
        SAXReader reader = new SAXReader();
        Document document = reader.read(inputStream);
        //得到xml根元素
        Element root = document.getRootElement();
        // 得到根元素的所有子节点
        List<Element> elementList = root.elements();
        //遍历所有子节点
        for (Element e : elementList) {
            map.put(e.getName(), e.getText());
        }
        //释放资源
        inputStream.close();
        return map;
    }

    /**
     * 将String类型的XML解析为Map
     *
     * @param str
     * @return
     * @throws Exception
     */
    public static Map<String, String> parseXmlStringToMap(String str) throws Exception {
        Map<String, String> resultMap;
        InputStream inputStream = new ByteArrayInputStream(str.getBytes("UTF-8"));
        resultMap = parseInputStreamToMap(inputStream);
        return resultMap;
    }


}
